package org.dromara.pill.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.pill.domain.vo.PillFactoryVo;
import org.dromara.pill.domain.bo.PillFactoryBo;
import org.dromara.pill.service.IPillFactoryService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 生产厂家信息
 *
 * @author Lion Li
 * @date 2023-07-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pill/factory")
public class PillFactoryController extends BaseController {

    private final IPillFactoryService pillFactoryService;

    /**
     * 查询生产厂家信息列表
     */
    @SaCheckPermission("pill:factory:list")
    @GetMapping("/list")
    public TableDataInfo<PillFactoryVo> list(PillFactoryBo bo, PageQuery pageQuery) {
        return pillFactoryService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出生产厂家信息列表
     */
    @SaCheckPermission("pill:factory:export")
    @Log(title = "生产厂家信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PillFactoryBo bo, HttpServletResponse response) {
        List<PillFactoryVo> list = pillFactoryService.queryList(bo);
        ExcelUtil.exportExcel(list, "生产厂家信息", PillFactoryVo.class, response);
    }

    /**
     * 获取生产厂家信息详细信息
     *
     * @param factoryId 主键
     */
    @SaCheckPermission("pill:factory:query")
    @GetMapping("/{factoryId}")
    public R<PillFactoryVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long factoryId) {
        return R.ok(pillFactoryService.queryById(factoryId));
    }

    /**
     * 新增生产厂家信息
     */
    @SaCheckPermission("pill:factory:add")
    @Log(title = "生产厂家信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PillFactoryBo bo) {
        return toAjax(pillFactoryService.insertByBo(bo));
    }

    /**
     * 修改生产厂家信息
     */
    @SaCheckPermission("pill:factory:edit")
    @Log(title = "生产厂家信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PillFactoryBo bo) {
        return toAjax(pillFactoryService.updateByBo(bo));
    }

    /**
     * 删除生产厂家信息
     *
     * @param factoryIds 主键串
     */
    @SaCheckPermission("pill:factory:remove")
    @Log(title = "生产厂家信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{factoryIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] factoryIds) {
        return toAjax(pillFactoryService.deleteWithValidByIds(List.of(factoryIds), true));
    }
}
